package com.csu.petstore.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.petstore.common.CommonResponse;
import com.csu.petstore.mapper.*;
import com.csu.petstore.pojo.*;
import com.csu.petstore.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.Name;
import java.time.LocalDate;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderstatusMapper orderstatusMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private SequenceMapper sequenceMapper;
    @Autowired
    private InventoryMapper inventoryMapper;


    @Override
    public CommonResponse<Order> getOrder(String orderId) {
        Order orders = orderMapper.selectById(orderId);
        return CommonResponse.createForSuccess(orders);
    }
    @Override
    public  CommonResponse<String> insertOrder(OrderVO orderVO){
        orderVO.getOrder().setOrderId(getNextId("ordernum"));
        for (int i = 0; i < orderVO.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) orderVO.getLineItems().get(i);
            Inventory inventory = new Inventory();
            inventory.setItemId(lineItem.getItemId());
            inventory.setQuantity(lineItem.getQuantity());

        }
        System.out.println(orderVO.getOrder().getOrderId());
        orderMapper.insert(orderVO.getOrder());

        //orderstatus
        LocalDate currentDate = LocalDate.now();
        Orderstatus orderstatus = new Orderstatus();
        orderstatus.setOrderid(orderVO.getOrder().getOrderId());
        orderstatus.setLinenum(50000);
        orderstatus.setStatus("P");
        orderstatus.setTimestamp(currentDate);
        orderstatusMapper.insert(orderstatus);

        //lineItem
        List<CartItem> itemList = orderVO.getCart().getItemList();
        if (itemList != null && !itemList.isEmpty()) {
            // 遍历购物车中的每个商品项，转换为订单行项目并插入到数据库
            for (CartItem cartItem : itemList) {
                LineItem lineItem = new LineItem();
                lineItem.setOrderId(orderVO.getOrder().getOrderId());
                lineItem.setLinenum(1);
                lineItem.setItemId(cartItem.getItemId());
                lineItem.setQuantity(cartItem.getQuantity());
                lineItem.setUnitPrice(cartItem.getUnitPrice());
                lineItem.setPrice(cartItem.getListPrice());
                // 插入订单行项目到数据库
                lineItemMapper.insert(lineItem);
            }
        }
        return CommonResponse.createForSuccess("Order inserted successfully");
    }
    @Transactional
    public int getNextId(String name) {

        Sequence sequence = new Sequence();
        sequence.setName(name);
        sequence.setNextId(-1);

        // 从数据库中获取序列
        QueryWrapper<Sequence> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Sequence retrievedSequence = sequenceMapper.selectOne(queryWrapper);
        if (retrievedSequence == null) {
            throw new RuntimeException("错误：未找到名称为 " + name + " 的序列");
        }

        // 递增序列并更新到数据库
        int nextId = retrievedSequence.getNextId() + 1;
        retrievedSequence.setNextId(nextId);
        sequenceMapper.updateById(retrievedSequence);

        return nextId;
    }

}
